package testenum;

import java.lang.reflect.Constructor;

public class TestReflectEnum {
    public static void reflectPrivateConstructor() {
        try {
            Class<?> classStudent = Class.forName("testenum.TestEnum");

            //注意传入对应的参数,获得对应的构造方法来构造对象,当前枚举类是提供了两个参数分别是String和int。
            Constructor<?> declaredConstructorStudent = classStudent.getDeclaredConstructor(String.class,int.class,String.class,int.class);

            //设置为true后可修改访问权限
            declaredConstructorStudent.setAccessible(true);

            // 用获取到的构造方法构造对象
            Object objectStudent = declaredConstructorStudent.newInstance("父类参数",111,"绿色",666);
            TestEnum testEnum = (TestEnum) objectStudent;

            System.out.println("获得枚举的私有构造函数："+testEnum);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {
        // 枚举通过反射不能拿到实例对象
        reflectPrivateConstructor();
    }
}
